package com.wzy.service.Impl;

import com.wzy.dao.AddressDao;
import com.wzy.entity.Address;
import com.wzy.entity.PageBean;
import com.wzy.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressDao addressDao;

    @Override
    public Address findByAid(int aid) {
        return null;
    }

    @Override
    public PageBean<Address> findAddress(int current, int size, int uid) {
        int begin=(current -1)*size;
        //查询1页的数据
        List<Address> data = addressDao.findByUid(uid, begin, size);
        //查询总记录数
        int count = addressDao.findCount(uid);
        //封装成PageBean对象返回
        return new PageBean<>(data,count,current,size);
    }

    /**
     * 保存地址
     * @param address
     */
    @Override
    public void save(Address address) {
        //设置默认地址为0
        address.setIsdefault("0");
        //调用DAO添加地址
        addressDao.save(address);
    }

    @Override
    public void update(Address address) {
            addressDao.update(address);
    }

    @Override
    public void delete(Integer aid) {
       addressDao.delete(aid);
    }

    @Override
    @Transactional
    public void changeDefaultAddress(Integer uid, Integer aid) {
    addressDao.updateAddressIsNotDefault(uid);
    addressDao.updateAddressIsDefault(aid);
    }
}
